﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;
using MyPS.Models;

namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageResourcesController : Controller
    {
        private MyPSEntities db = new MyPSEntities();

        //
        // GET: /Manage/ManageResources/

        public ActionResult Index()
        {
            return View(db.Resources.ToList());
        }

        //
        // GET: /Manage/ManageResources/Details/5

        public ActionResult Details(int id = 0)
        {
            Resource resource = db.Resources.Find(id);
            if (resource == null)
            {
                return HttpNotFound();
            }
            return View(resource);
        }

        //
        // GET: /Manage/ManageResources/Create

        public ActionResult Create()
        {
            ViewBag.TypeList = new SelectList(db.ResourceTypes.ToList(), "Descr", "Descr");
            return View();
        }

        //
        // POST: /Manage/ManageResources/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Resource resource)
        {
            if (ModelState.IsValid)
            {
                resource.ParentMode = "";
                resource.CreateBy = HttpContext.User.Identity.Name;
                resource.CreateDate = DateTime.Now;
                resource.ModifyBy = HttpContext.User.Identity.Name;
                resource.ModifyDate = DateTime.Now;
                db.Resources.Add(resource);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeList = new SelectList(db.ResourceTypes.ToList(), "Descr", "Descr");
            return View(resource);
        }

        //
        // GET: /Manage/ManageResources/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Resource resource = db.Resources.Find(id);
            if (resource == null)
            {
                return HttpNotFound();
            }
            ViewBag.TypeList = new SelectList(db.ResourceTypes.ToList(), "Descr", "Descr");
            return View(resource);
        }

        //
        // POST: /Manage/ManageResources/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Resource resource)
        {
            if (ModelState.IsValid)
            {
                var m = db.Resources.Single(o => o.ResourceID == resource.ResourceID); 
                m.ParentMode = "";
                m.ModifyBy = HttpContext.User.Identity.Name;
                m.ModifyDate = DateTime.Now;
                m.Mode = resource.Mode;
                m.ResourceName = resource.ResourceName;
                m.Url = resource.Url;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(resource);
        }

        //
        // GET: /Manage/ManageResources/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Resource resource = db.Resources.Find(id);
            if (resource == null)
            {
                return HttpNotFound();
            }
            return View(resource);
        }

        //
        // POST: /Manage/ManageResources/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Resource resource = db.Resources.Find(id);
            db.Resources.Remove(resource);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ResourceTypeList()
        {
            return View(db.ResourceTypes.ToList());
        }

        public ActionResult CreateResourceType()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreateResourceType(ResourceType type)
        {
            if (ModelState.IsValid)
            {
                db.ResourceTypes.Add(type);
                db.SaveChanges();
                return RedirectToAction("ResourceTypeList");
            }
            return View(type);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}